const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar, //模态动画
    CustomBar: app.globalData.CustomBar,
    id:'',
    index:'',
    show:true,
    list:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    wx.setNavigationBarTitle({
      title: '收货地址',
    })
    if (options.type == 'order'){
      that.setData({
        show:false
      })
    }
  },

  //模态动画
  showModal(e) {
    const that = this;
    const index = e.currentTarget.dataset.index;
    const data = that.data.list;
    const id = data[index]['id'];
    that.setData({
      modalName: e.currentTarget.dataset.target,
      index: index,
      id: id
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  delModal(e){
    const that = this;
    const index = that.data.index;
    const data = that.data.list;
    app.util.request({
      url: 'Index/Index/delAddress',
      data: {
        openid: app.globalData.userInfo.openid,
        id:that.data.id
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        data.splice(index, 1);
        that.setData({
          modalName: null,
          list: data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(){
    const that = this;
    app.util.request({
      url: 'Index/Index/getAddress',
      data: {
        openid: app.globalData.userInfo.openid
      },
      header: {
        'content-type': 'application/json'
      },
      success(res){
        that.setData({
          list:res.data
        })
      }
    })
  },

  getDefault:function(e){
    const that = this;
    const index = e.target.dataset.index;
    const data = that.data.list;
    app.util.request({
      url: 'Index/Index/changeStatic',
      data:{
        openid: app.globalData.userInfo.openid,
        id:data[index]['id']
      },
      method: 'post',
      header:{
        'content-type': 'application/json'
      },
      success(res) {
        for (var i = 0; i < data.length; i++){
          data[i]['default'] = 0;
        }
        data[index]['default'] = 1;
        that.setData({
          list: data
        })
      }
    })
  },

  getDefaults:function(e){
    const that = this;
    const index = e.target.dataset.index;
    const data = that.data.list;
    app.util.request({
      url: 'Index/Index/changeStatic',
      data: {
        openid: app.globalData.userInfo.openid,
        id: data[index]['id']
      },
      method: 'post',
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        for (var i = 0; i < data.length; i++) {
          data[i]['default'] = 0;
        }
        data[index]['default'] = 1;
        that.setData({
          list: data
        })
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
          address: data[index]['address'],
          latitude: data[index]['latitude'],
          longitude: data[index]['longitude'],
          phone: data[index]['phone'],
          name: data[index]['name']
        });
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },

  editAddress:function(e){
    const that = this;
    const index = e.currentTarget.dataset.index;
    const data = that.data.list;
    var id = data[index]['id'];
    wx.navigateTo({
      url: '../addNewAddress/addNewAddress?id='+id
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})